package com.tsing.cedap.rest.controller;


import com.tsing.cedap.common.annotations.Limit;
import com.tsing.cedap.common.annotations.LimitType;
import com.tsing.cedap.common.annotations.PassToken;
import com.tsing.cedap.common.annotations.SystemLog;
import com.tsing.cedap.common.utils.ResultUtil;
import com.tsing.cedap.common.vo.ResultData;
import com.tsing.cedap.rest.service.IUserService;
import com.tsing.cedap.rest.vo.reqVo.CodeReqVo;
import com.tsing.cedap.rest.vo.reqVo.MiniRegisterReqVo;
import com.tsing.cedap.rest.vo.reqVo.PerfectUserInfoReqVo;
import com.tsing.cedap.rest.vo.reqVo.VerifyCodeReqVo;
import com.tsing.cedap.rest.vo.respVo.LoginRespVo;
import com.tsing.cedap.rest.vo.respVo.MiniIndexStatisticRespVo;
import com.tsing.cedap.rest.vo.respVo.SendMessageRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TheTsing
 * @since 2022-01-10
 */
@Api(tags = "用户登录模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PassToken
    @Limit(period = 1000, count = 1, limitType = LimitType.METHODIP)
    @PostMapping("/miniLogin")
    @ApiOperation(value = "小程序登录", notes = "错误代码为811时，请先注册")
    @SystemLog(description = "小程序登录")
    public ResultData<LoginRespVo> miniLogin(@Validated @RequestBody CodeReqVo reqVo) {
        return ResultUtil.success(userService.miniLogin(reqVo));
    }

    @PassToken
    @Limit(period = 60000, count = 1, limitType = LimitType.METHODIP)
    @PostMapping("/miniSendMessage")
    @ApiOperation(value = "小程序发送短信验证码", notes = "验证码有效期为10分钟，发送间隔1分钟以上")
    @SystemLog(description = "小程序发送短信验证码")
    public ResultData<SendMessageRespVo> miniSendMessage(@Validated @RequestBody CodeReqVo reqVo) {
        return ResultUtil.success(userService.miniSendMessage(reqVo));
    }

    @PassToken
    @PostMapping("/verifyCode")
    @ApiOperation(value = "验证短信验证码")
    @SystemLog(description = "验证短信验证码")
    public ResultData verifyCode(@Validated @RequestBody VerifyCodeReqVo reqVo) {
        userService.verifyCode(reqVo);
        return ResultUtil.success();
    }

    /**
     * 先判断 user_auth 表有无 openid 为 当前 openid 的条目
     * 如果有，直接抛出异常或者返回 token
     * 如果无，判断 use 表中是否有手机号为当前手机号的条目
     * 如果有，直接插入一条登录方式
     * 如果无，插入一条在 use 表，同时插入两条到 user_auth 表
     */
    @PassToken
    @Limit(period = 1000, count = 1, limitType = LimitType.METHODIP)
    @PostMapping("/miniRegister")
    @ApiOperation(value = "注册小程序")
    @SystemLog(description = "注册小程序")
    public ResultData<LoginRespVo> miniRegister(@Validated @RequestBody MiniRegisterReqVo reqVo) {
        return ResultUtil.success(userService.miniRegister(reqVo));
    }

    @Limit(period = 1000, count = 1, limitType = LimitType.METHODIP)
    @PostMapping("/perfectUserInfo")
    @ApiOperation(value = "用户基本信息完善")
    @SystemLog(description = "用户基本信息完善")
    public ResultData perfectUserInfo(@Validated @RequestBody PerfectUserInfoReqVo reqVo, HttpServletRequest request) {
        userService.perfectUserInfo(reqVo, request);
        return ResultUtil.success();
    }

    @GetMapping("/miniCustomerStatistic")
    @ApiOperation(value = "小程序首页客户信息统计")
    @SystemLog(description = "小程序首页客户信息统计")
    public ResultData<MiniIndexStatisticRespVo> miniCustomerStatistic(HttpServletRequest request) {
        return ResultUtil.success(userService.miniCustomerStatistic(request));
    }

    @GetMapping("/miniRiderStatistic")
    @ApiOperation(value = "小程序首页服务人员（骑手）信息统计")
    @SystemLog(description = "小程序首页服务人员（骑手）信息统计")
    public ResultData<MiniIndexStatisticRespVo> miniRiderStatistic(HttpServletRequest request) {
        return ResultUtil.success(userService.miniRiderStatistic(request));
    }

}
